const app = getApp()
const WXAPI = require("../../../../../api/newApi")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '我的会员码', // 导航栏 中间的标题
      textColor: '#fff', // 标题颜色
      bgColor: '', // 导航栏背景颜色
      isColor: false,
    },
    info: {}, //信息
    time: 120, //倒计时时间
    isRefresh: false, //是否刷新
    isScanCode: false, //是否已经扫码
    customerCardId:'',//用户卡id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.customerCardId = options.customerCardId
    this.getQrCode()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.getQrCode()
  },
  // 刷新重试
  getRefresh() {
    clearInterval(this.data.timer)
    clearInterval(this.data.codeTime)
    this.setData({
      isRefresh: true,
      time: 120,
    })
    this.getQrCode()
  },
  //获取会员码
  getQrCode() {
    clearInterval(this.data.timer)
    var url = `/app/v2/customer/asset/card/qrcode/${this.data.customerCardId}`
    WXAPI.cardQrcode(url, '', res => {
      this.setData({
        info: res.result,
        isScanCode: false,
      })
      this.circulateGetQrcodeScan()
      setTimeout(() => {
        this.setData({
          isRefresh: false,
        })
      }, 1000)
      this.countdownView()
    })
  },
  //轮询请求二维码使用情况
  circulateGetQrcodeScan(){
    var that = this
    this.setData({
      codeTime: setInterval(function () {
        that.getQrcodeScan()
      }, 2000)
    })
  },
  //查询二维码使用情况
  getQrcodeScan() {
    var data = {
      query: this.data.info.query
    }
    WXAPI.qrcodeScan(data, (res) => {
      this.setData({
        isScanCode: res.result
      })
      if (res.result) {
        clearInterval(this.data.codeTime);
      }
    })
  },
  //倒计时
  countdownView() {
    var that = this
    this.setData({
      timer: setInterval(function () {
        that.data.time--;
        that.setData({
          time: that.data.time,
        })
        if (that.data.time == 0) {
          clearInterval(that.data.timer);
          that.setData({
            time: 120
          })
          that.getQrCode()
        }
      }, 1000)
    })
  },
  bindscroll(e) { //监听页面滚动距离
    var that = this;
    var scrollY = e.detail.scrollTop
    if (scrollY > 50) {
      that.setData({
        ['nvabarData.textColor']: '#000',
        ['nvabarData.isScrollY']: true,
        ['nvabarData.isColor']: true,
      })
      wx.setNavigationBarColor({
        frontColor: '#000000',
        backgroundColor: '#000000',
      })
    } else if (scrollY < 50) {
      that.setData({
        ['nvabarData.textColor']: '#fff',
        ['nvabarData.isScrollY']: false,
        ['nvabarData.isColor']: false,
      })
      wx.setNavigationBarColor({
        frontColor: '#ffffff',
        backgroundColor: '#ffffff',
      })
    }
  },
  onUnload() {
    console.log('页面卸载')
    clearInterval(this.data.timer)
    clearInterval(this.data.codeTime)
    this.setData({
      time: 120
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh() {

  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom() {

  // },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})